package org.amn.file;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.amn.config.AppConfig;

/**
 * <p>
 * 描述:管理文件目录,清理目录,清理策略等
 * </p>
 *
 * @see
 * @author yangjun1120@gmail.com
 *
 */
public class FileManager {

    //private final static String DIRECTORIES = "pend,queue,success,exception,backup";

    private final static String PATH_PENDDING = "pend";
    private final static String PATH_ENQUEUE = "queue";
    private final static String PATH_SUCCESS = "success";
    private final static String PATH_EXCEPTION = "exception";
    private final static String PATH_BACKUP = "backup";

    //    private String prefixDir;
    //    private String bakPrefixDir;

    private Map<FileStatus, String> dirMap = Collections.synchronizedMap(new HashMap<FileStatus, String>());

    //File timeout
    //private long timeout;
    private final static TimeUnit timeUnit = TimeUnit.SECONDS;

    private AppConfig config;

    //    public String getPrefixDir() {
    //        return prefixDir;
    //    }

    public TimeUnit getTimeUnit() {
        return timeUnit;
    }

    public FileManager(AppConfig appConfig) throws IOException {
        //        this.prefixDir = FileUtils.formatPath(prefixDir);
        //        this.bakPrefixDir = FileUtils.formatPath(bakPrefixDir);
        //        this.timeout = timeout;
        //        this.timeUnit = unit;
        this.config = appConfig;

        init();
    }

    public Map<FileStatus, String> getDirMap() {
        return dirMap;
    }

    public long getFileTimeout() {
        return config.getFileTimeout();
    }

    public long getFileBackupTime() {
        return config.getFileBackupTime();
    }

    public long getEmptyPathTimeout() {
        return config.getEmptyPathTimeout();
    }

    //    public long getTimeout() {
    //        return timeout;
    //    }
    //
    //    public void setTimeout(long timeout) {
    //        this.timeout = timeout;
    //    }

    //    public TimeUnit getTimeUnit() {
    //        return timeUnit;
    //    }
    //
    //    public void setTimeUnit(TimeUnit timeUnit) {
    //        this.timeUnit = timeUnit;
    //    }

    //    public String getBakPrefixDir() {
    //        return bakPrefixDir;
    //    }

    public void init() throws IOException {
        //1.按状态分总目录

        dirMap.put(FileStatus.PENDDING, FileUtils.formatPath(config.getPrefixDir() + File.separator + PATH_PENDDING));
        dirMap.put(FileStatus.ENQUEUE, FileUtils.formatPath(config.getPrefixDir() + File.separator + PATH_ENQUEUE));
        dirMap.put(FileStatus.SUCCESS, FileUtils.formatPath(config.getPrefixDir() + File.separator + PATH_SUCCESS));
        dirMap.put(FileStatus.EXCEPTION, FileUtils.formatPath(config.getPrefixDir() + File.separator + PATH_EXCEPTION));
        dirMap.put(FileStatus.BACKUP, FileUtils.formatPath(config.getBakPrefixDir() + File.separator + PATH_BACKUP));

        for (String p : dirMap.values()) {
            File f = new File(p);
            if (!f.exists()) {

                if (!f.mkdirs()) {
                    throw new IOException("can't create path:" + p);
                }

            }
        }

    }

    //清理相关目录文件,移动相关文件。backup
    public void clear() {

    }

    //清理所有建立的资源，释放资源，包括线程，目录等
    public void destroy() {

    }

    public String getPath(FileStatus s) {
        return dirMap.get(s);
    }

    public String[] getPenddingSubDir() {
        String path = getPath(FileStatus.PENDDING);
        return FileUtils.listDirFilePath(path);

    }

    public String[] getEnqueueSubDir() {
        String path = getPath(FileStatus.ENQUEUE);
        return FileUtils.listDirFilePath(path);
    }

    public static void main(String[] args) throws Exception {
        String[] s = new String[0];

        System.out.println(s == null);

    }

}
